<template>
	<div class="news w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<p><i class="line"></i>新闻资讯</p>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<a class="listOne" @click="createNews" v-if="news.xwzx_add">新增资讯</a>
				<a class="listTwo" @click="importDaoChu" v-if="news.xwzx_export">导出数据</a>
				<el-cascader v-model="searchSelect" :options="options" :props="optionProps" :show-all-levels='false' filterable placeholder="资讯分类" class="th_2" @change="getTableList"></el-cascader>
				<div class="msearch">
					<input placeholder="请输入资讯关键词..." v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column prop="courseName" label="新闻标题"></el-table-column>
				<el-table-column label="操作" width="208">
					<template slot-scope="scope">
						<el-button @click="handleEdit(scope.row)" v-if="news.xwzx_edit">编辑</el-button>
						<el-button @click="handlecomments(scope.row)" v-if="news.xwzx_comment" class="blue">评论</el-button>
						<el-button @click="handleDelete(scope.row)" v-if="news.xwzx_del" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll()" class="allDelete" v-if="news.xwzx_delAll">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				news:'',
				search:'',
				tableData:[],
				pagesize: 15,
				currentpage: 1,
				total: 1,
				multipleSelection: [],
				deleteType:'delete',
				searchSelect: [],
				options: [],
				optionProps: {
				    value: 'id',
				    label: 'columnName',
				    children: 'childList'
				}
			}
		},
		methods: {
			getTableList(){
				var s={
				    page: this.currentpage,
					num: this.pagesize,
					fuzzyFields: this.search,
					rootcompanyid:1,
					courseType:2
                };
				if(this.searchSelect!=null){
                	s.courseColumnYi = this.searchSelect[0];
					s.courseColumn = this.searchSelect[1];
                };
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/kcCourse/selectKcCourseList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: s,
					success: res => {
						if(res.data.code == 200) { 
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
						}
					}
				})
			},
			handleEdit(row) {
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/kcCourse/getCourseDetails",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {id:row.id},
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'addnews',
								params: {
									type: 'edit'
								}
							})
							this.$root.newMsg = res.data.data;
//							console.log('102',this.$root.managerMsg)
						}else{
							console.log("获取详细信息失败")
						}
					}
				});
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var list = [];
					list.push({id:this.transitionRow.id});
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourse/deleteKcCourseState",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourse/deleteKcCourseState",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:this.multipleSelection,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			handlecomments(row) {
				this.$router.push({
					name: 'addcomment',
					params: {
						courseId: row.id,
						courseName:row.courseName
					} 
				})
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				for(var i = 0;i<val.length;i++){
					var s={id:val[i].id,courseRecommend:val[i].courseRecommend}
					this.multipleSelection.push(s)
				}
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			createNews() {
				this.$router.push({
					path: '/addnews'
				})
			},
			getTreeData(data){
			    for(var i=0;i<data.length;i++){
			        if(data[i].childList.length<1){
			            data[i].childList=undefined;
			        }else {
			            this.getTreeData(data[i].childList);
			        }
			    }
			    return data;
			},
			importDaoChu(){
				
			},
			getColumnList(){
				let columnType = [2];
				this.$rootFunction().columnList({columnType});
			}
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getTableList();
			this.getColumnList();
			this.news = this.$root.huasan.news;
		}
	}
</script>